package andris.android.mobilarena.activity;

import andris.android.mobilarena.MobilarenaApplication;
import andris.android.mobilarena.R;
import andris.android.mobilarena.provider.MainPagerAdapter;
import android.app.ActionBar;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.viewpagerindicator.TitlePageIndicator;

public class MainActivity extends FragmentActivity {

	private ViewPager mViewPager;
	private TitlePageIndicator mTitleIndicator;
	private LinearLayout mLayout;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState); 
		mLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.main, null);
		setContentView(mLayout);

		mViewPager = (ViewPager) mLayout.findViewById(R.id.main_viewpager);
		MainPagerAdapter mAdapter = new MainPagerAdapter(getSupportFragmentManager());
		mViewPager.setAdapter(mAdapter);
		mViewPager.setCurrentItem(mAdapter.getCenter());

		mTitleIndicator = (TitlePageIndicator) mLayout.findViewById(R.id.main_titlepageindicator);
		mTitleIndicator.setViewPager(mViewPager);

		// conditional ICS code
		if (MobilarenaApplication.getCurrentApiVersion() >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
			ActionBar mActionbar = getActionBar();
			mActionbar.setTitle("Mobilarena");
		}
	}

	public void toggleViewPager(boolean b){
		mViewPager.setEnabled(b);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu, menu);
		// compat package para
		//
		// if (MobilarenaApplication.getCurrentApiVersion() >=
		// android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
		// SearchView mSearchView = (SearchView)
		// menu.findItem(R.id.menu_main_search).getActionView();
		// mSearchView.setOnQueryTextListener(new OnQueryTextListener() {
		//
		// @Override
		// public boolean onQueryTextSubmit(String query) {
		// System.out.println(query);
		// return true;
		// }
		//
		// @Override
		// public boolean onQueryTextChange(String newText) {
		// // TODO Auto-generated method stub
		// return false;
		// }
		// });
		// }
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		Toast.makeText(MainActivity.this, item.getTitle(), Toast.LENGTH_LONG).show();
		return true;
	}
}